// copyright 1993 Michael B. Johnson; some portions copyright 1994, MIT
// see COPYRIGHT for reuse legalities
//

#import "RIBSkew.h"

@implementation RIBSkew

+ initialize { return [RIBSkew setVersion:1], self; }

- init
{
  [super init];
  angle = 90.0;
  dx1 = 1.0;
  dy1 = 0.0;
  dz1 = 0.0;
  dx2 = 1.0;
  dy2 = 0.0;
  dz2 = 0.0;

  return self;
}

- awake
{
  [super awake];
  N3D_CopyMatrix(N3DIdentityMatrix, myMatrix);
  NXLogError("should be updating the skew matrix - but I'm not...\n");

  return self;
}

- setAngle:(RtFloat)newAngle dx1:(RtFloat)newDX1 dy1:(RtFloat)newDY1 dz1:(RtFloat)newDZ1 dx2:(RtFloat)newDX2 dy2:(RtFloat)newDY2 dz2:(RtFloat)newDZ2
{
  angle = newAngle;
  dx1 = newDX1;
  dy1 = newDY1;
  dz1 = newDZ1;
  dx2 = newDX2;
  dy2 = newDY2;
  dz2 = newDZ2;

  N3D_CopyMatrix(N3DIdentityMatrix, myMatrix);

  return self;
}

- getAngle:(RtFloat *)anglePtr 
     dx1:(RtFloat *)dx1Ptr dy1:(RtFloat *)dy1Ptr dz1:(RtFloat *)dz1Ptr 
     dx2:(RtFloat *)dx2Ptr dy2:(RtFloat *)dy2Ptr dz2:(RtFloat *)dz2Ptr;
{
  *anglePtr = angle;
  *dx1Ptr = dx1;
  *dy1Ptr = dy1;
  *dz1Ptr = dz1;
  *dx2Ptr = dx2;
  *dy2Ptr = dy2;
  *dz2Ptr = dz2;

  return self;
}

- renderSelf:(WW3DCamera *)camera  startingAt:(RtFloat)shutterOpenTime endingAt:(RtFloat)shutterCloseTime
{
  RiSkew(angle, dx1, dy1, dz1, dx2, dy2, dz2);

  return self;
}

- transformCTM:(WW3DAttributeState *)attributeState startingAt:(RtFloat)shutterOpenTime endingAt:(RtFloat)shutterCloseTime
{ 
  RtMatrix  aMatrix, tmpMatrix;


  NXLogError("should be skewing the CTM - but I'm not...\n");
  [attributeState getTransformMatrix:aMatrix];
  N3DMultiplyMatrix(myMatrix, aMatrix, tmpMatrix);
  [attributeState setTransformMatrix:tmpMatrix];

  return self; 
}

- (BOOL)isMotionBlurrable { return YES; }

- writeEve:(NXStream *)stream atTabLevel:(int)tab
{
   int  i;


   for (i = 0; i < tab; i++)
   {  NXPrintf(stream, "\t");
   }
   NXPrintf(stream, "Skew %f %f %f %f %f %f %f;", angle, dx1, dy1, dz1, dx2, dy2, dz2); 
   return self;
}

#define typeVector "ffff"
#define typeValues &angle, &dx1, &dy1, &dz1, &dx2, &dy2, &dz2

- read:(NXTypedStream*)stream 
{
    int version;
    [super read:stream];

    version = NXTypedStreamClassVersion(stream,"RIBSkew");
    if (version == 0) NXReadTypes(stream,"i",&version), version=1;
    if (version == 1)
    {  NXReadTypes(stream,typeVector,typeValues);
    } 
    else 
    {
    }
    return self;
}

- write:(NXTypedStream*)stream 
{
    [super write:stream];

    NXWriteTypes(stream,typeVector, typeValues);

    return self;
}

@end
